﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Data.SqlClient;
using ISmart;
using Wee;
using System.Text;

namespace SmartServer
{
    /// <summary>
    /// 用户相关服务
    /// </summary>
    [WebService(Namespace = "weetry.com")]
    [ScriptService()]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Users : System.Web.Services.WebService
    {
        /// <summary>
        /// 获取用户信息列表
        /// </summary>
        /// <param name="asspUserId">API用户ID</param>
        /// <param name="keyCode">验证密钥</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="isAccurate">是否精确查询</param>
        /// <param name="userName">用户名</param>
        /// <param name="state">当前状态</param>
        /// <returns></returns>
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public List<ISmart.Models.UserItem> GetUserList(string asspUserId, string keyCode, int currentPage, int pageSize, bool isAccurate, string realName, WeeBool state)
        {
            List<ISmart.Models.UserItem> ui = new List<ISmart.Models.UserItem>();

            using (SqlCommand cmd = new SqlCommand("pPAGING", new SqlConnection(SqlHelper.APIConnection)))
            {
                StringBuilder filterString = new StringBuilder("1 = 1");
                SqlDataReader dr;

                if (realName != "")
                {
                    if (isAccurate)
                    {
                        filterString.AppendFormat(" AND RealName = '{0}'", ParameterList.DBConvert(realName, SqlDbType.NVarChar, false, 10));
                    }
                    else
                    {
                        filterString.AppendFormat(" AND RealName LIKE '%{0}%'", ParameterList.DBConvert(realName, SqlDbType.NVarChar, false, 10));
                    }
                }//若UserName不为空

                if (state != WeeBool.Null)
                {
                    filterString.AppendFormat(" AND State = {0}", (bool)ParameterList.DBConvert(state, SqlDbType.Bit, false) ? 1 : 0);
                }

                cmd.Connection.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(ParameterList.Add("@Filter", SqlDbType.VarChar, filterString, 1000));
                cmd.Parameters.Add(ParameterList.Add("@Tables", SqlDbType.VarChar, "N_Users", 1000, true));
                cmd.Parameters.Add(ParameterList.Add("@PrimaryKey", SqlDbType.VarChar, "UID", 100, true));
                cmd.Parameters.Add(ParameterList.Add("@CurrentPage", SqlDbType.Int, currentPage, true));
                cmd.Parameters.Add(ParameterList.Add("@PageSize", SqlDbType.Int, pageSize, true));
                cmd.Parameters.Add(ParameterList.Add("@Fields", SqlDbType.VarChar, "UID, UserName, RealName, Tel, QQ, Email, State", 1000));
                dr = cmd.ExecuteReader();
                cmd.CommandType = CommandType.StoredProcedure;

                while (dr.Read())
                {
                    ui.Add(new ISmart.Models.UserItem()
                    {
                        UID = (int)dr["UID"],
                        UserName = dr["UserName"].ToString(),
                        RealName = dr["RealName"].ToString(),
                        Tel = dr["Tel"].ToString(),
                        QQ = dr["QQ"].ToString(),
                        Email = dr["Email"].ToString(),
                        State = dr["State"].ToString()
                    });
                }

                cmd.Connection.Close();
            }

            //调用存储过程
            return ui;
        }
    }
}
